<?php

/**
 * Description of Acl
 *
 * @author gun
 */
class Admin_Model_DbTable_Acl extends Zend_Db_Table_Abstract {
    protected $_name = 'acl';
    protected $_id = 'aclID';
    
    public function findRow($groupID) {
        $sql = $this->getAdapter()->select()
                ->from(array('a'=>$this->_name), array('aclID', 'groupID', 'menuID', 'status'))
                ->joinLeft(array('b'=>'menu'), 'a.menuID = b.menuID', array('name as menu_name', 'link'))
                ->joinLeft(array('c'=>'group'), 'a.groupID = c.groupID', array('name as group_name'))
                ->where('a.groupID = ?', $groupID);        
        
        return $this->_db->fetchRow($sql);
    }
    
    public function findAll($where, $options) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name);                
        
        if (is_array($where)) {
            foreach($where as $key=>$value) {
                $sql->where($key, $value);
            }
        }
        
        if (is_array($options)) {
            if (isset($options['limit']))
                foreach ($options['limit'] as $key => $value) {
                    $sql->limit($key, $value);
                }
        }
        
        return $this->_db->fetchAll($sql);
    }
    
    public function countRows($where) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name, 'COUNT(aclID)');                
        
        if (is_array($where)) {
            foreach($where as $key=>$value) {
                $sql->where($key, $value);
            }
        }
            
        return $this->getAdapter()->fetchOne($sql);
    }
    
    public function doSave($data = array()) {
        return $this->getAdapter()->insert($this->_name, $data);
    }
    
    public function doUpdate($data, $where) {
        return $this->getAdapter()->update($this->_name, $data, $where);        
    }
    
    public function doDelete($where) {
        return $this->getAdapter()->delete($this->_name, array("$this->_id = ?" => $where));
    }
    
    public function doDeleteByMenuID($where) {
        return $this->getAdapter()->delete($this->_name, array("menuID = ?" => $where));
    }
    
    public function updateAcl($data, $where) {   
        try {
            $this->getAdapter()->beginTransaction();
            
            foreach ($data as $key=>$value) {
                $this->getAdapter()->update($this->_name, $value, $where[$key]);
            }
            
            $save = $this->getAdapter()->commit();
            
            return $save;
            
        } catch (Exception $e) {            
            $this->getAdapter()->rollBack();
        }                                     
    }
}

?>
